﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace CSEPT.Controllers
{
    public class PhotoController : Controller
    {

        [LoggedIn]
        public ActionResult Add(int EventID)
        {
            Models.PhotoViewModel PVM = new Models.PhotoViewModel();
            PVM.Evnt = PVM.dal.GetEvent(EventID);
            return View(PVM);
        }
        [LoggedIn]
        public ActionResult Upload(HttpPostedFileBase[] Files, int EventID)
        {
            Models.PhotoViewModel PVM = new Models.PhotoViewModel();
            int FileID = 0;
            try
            {
                foreach (HttpPostedFileBase File in Files)
                {
                    Photo Phto = new Photo(EventID, String.Empty, PVM.CurrUser.ID);
                    
                    FileID = PVM.dal.UpdatePhoto(Phto);

                    Image i = Image.FromStream(File.InputStream, true, true);
                    i.Save(Server.MapPath("~/EventPhotos/" + FileID.ToString() + "_raw.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
                    ResizeAndSave(i, 800, FileID, "thumbnail");
                    ResizeAndSave(i, 1500, FileID, "preview");
                }
                return RedirectToAction("Event", "Photo", new { @EventID = EventID });
            }
            catch
            {

                PVM.dal.UpdatePhoto(new Photo(FileID));
                PVM.Evnt = PVM.dal.GetEvent(EventID);
                PVM.PageMessage = "An error occured, did you select an image?";
                PVM.PageMessageType = "danger";
                return View("Add", PVM);
            }
        }
        [LoggedIn]
        public ActionResult Event(int EventID)
        {
            Models.PhotoViewModel PVM = new Models.PhotoViewModel();
            PVM.Evnt = PVM.dal.GetEvent(EventID);
            PVM.Photos = PVM.dal.GetEventPhotos(PVM.Evnt);
            return View(PVM);
        }
        [LoggedIn]
        public ActionResult View(int PhotoID)
        {
            try
            {
                Models.PhotoViewModel PVM = new Models.PhotoViewModel();
                    PVM.Photos = new List<Photo>();
                if (PhotoID == 0)
                {
                    Photo Phto = PVM.dal.GetRandomPhoto();
                    PVM.Photos.Add(Phto);
                }
                else
                {
                    Photo Phto = PVM.dal.GetPhoto(PhotoID);
                    PVM.Photos.Add(Phto);
                }
                return View(PVM);
            }
            catch (Exception)
            {
                return RedirectToAction("IndexInfo", "Index", new {@Message = "No Photos to tag at this time", @Type = "info"});
            }
        }
        [LoggedIn]
        public ActionResult Tag(int PhotoID, int EventID, string BibNumber)
        {
            Models.PhotoViewModel PVM = new Models.PhotoViewModel();
            string[] BibNumbers = BibNumber.Split(',');
            Tag tag = new Tag();
            tag.TaggedPhoto.ID = PhotoID;
            tag.TaggedBy = PVM.CurrUser;
            tag.TagEvent.ID = EventID;
            foreach (String BibNum in BibNumbers)
            {
                tag.BibNum = BibNum;
                PVM.dal.UpdateTag(tag);    
            }
            return RedirectToAction("View", "Photo", new { @PhotoID = 0 });
        }
        [LoggedIn]
        public FileResult Download(int PhotoID)
        {
            Models.PhotoViewModel PVM = new Models.PhotoViewModel();
            if (PVM.CurrUser.Credits > 2)
            {
                PVM.dal.SetTransaction(new Transaction(PVM.CurrUser, false, -3, "Downloaded photo: " + PhotoID.ToString()));
                byte[] file = System.IO.File.ReadAllBytes(Server.MapPath("~/EventPhotos/" + PhotoID.ToString() + "_raw.jpg"));
                return File(file, System.Net.Mime.MediaTypeNames.Application.Octet, "EventPhoto.jpg");
            }
            else
            {
                byte[] bytes = Encoding.UTF8.GetBytes("Sorry You do not have sufficient credits, you must tag more photos to get credits.");
                FileContentResult file = new FileContentResult(bytes, "text/text");
                file.FileDownloadName = "Insufficient Credits.txt";
                return file;
            }
        }
        [LoggedIn]
        public ActionResult Delete(int PhotoID, int EventID){
            Models.PhotoViewModel PVM = new Models.PhotoViewModel();
            Photo PhotoToDelete = PVM.dal.GetPhoto(PhotoID);
            if (PhotoToDelete.Photographer.ID != PVM.CurrUser.ID)
            {
                    return RedirectToAction("IndexInfo", "Index", new { @Message = "You cannot delete a photo you did not upload.", @Type = "warning" });
            }
            PVM.dal.UpdatePhoto(new Photo(PhotoID));
            return RedirectToAction("Event", "Photo", new { @EventID = EventID });
        }
        [LoggedIn]
        private void ResizeAndSave(Image i, int TargetWidth, int ID, string type)
        {
            
            int TargetHeight = Convert.ToInt32(Convert.ToDouble(i.Height) * (Convert.ToDouble(TargetWidth) / Convert.ToDouble(i.Width)));
            
            Image TempIMG = (Image)(new Bitmap(i, new Size(TargetWidth, TargetHeight)));
            TempIMG.Save(Server.MapPath("~/EventPhotos/" + ID.ToString() + "_" + type + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
        }

    }
}
